<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>文章列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/iconfont.css">
  <link rel="stylesheet" href="css/main.css">
  <script src="./libs/jquery-1.12.4.min.js"></script>
  <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  <!-- 引入模板引擎js文件 -->
  <script src="./libs/template-web.js"></script>
  <!-- 导入http地址 -->
  <script src="./libs/https.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      文章类别管理
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20 category_table">
        <thead>
          <tr>
            <th>名称</th>
            <th>Slug</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody>
          <!-- <tr>
            <td>奇趣事</td>
            <td>funny</td>
            <td class="text-center">
              <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
              <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr> -->
        </tbody>
        <tfoot>
          <tr>
            <td colspan="3" class="text-center">
              <a class="btn btn-success" id="xinzengfenlei" data-toggle="modal" data-target="#myModal"
                href="#myModal">新增分类</a>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>

  <!-- 模态框 -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
              aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="exampleModalLabel">New message</h4>
        </div>
        <div class="modal-body">
          <form>
            <div class="form-group">
              <label for="recipient-name" class="control-label category_name">类别名称</label>
              <input type="text" class="form-control" id="recipient-name">
            </div>
            <div class="form-group">
              <label for="message-text" class="control-label">slug</label>
              <input type="text" class="form-control" id="message-text">
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          <button type="button" class="btn btn-primary btn-save">Send message</button>
        </div>
      </div>
    </div>
  </div>

</body>

</html>
<script type="text/html" id="tpl-category">
{{each}}
<tr>
  <td>{{$value.name}}</td>
  <td>{{$value.slug}}</td>
  <td class="text-center">
      <a href="#myModal" dataid={{$value.id}} data-toggle="modal" class=" btn btn-info btn-xs btn-edit" data-toggle="modal"
      data-target="#myModal">编辑</a>
      <a href="#myModal" dataid={{$value.id}} class="btn btn-danger btn-xs btn_del">删除</a>
  </td>
</tr>
{{/each}}
</script>
<script>
  $(function () {
    // http://localhost:8080/api/v1
    // 请求地址：/admin/category/list
    // 封装函数 
    function loadData() {
      $.ajax({
        url: 'http://localhost:8080/api/v1/admin/category/list',
        success(res) {
          if (res.code === 200) {
            $('tbody').html(template('tpl-category', res.data))
          }
        }
      })
    }

    // 调用函数
    loadData()

    // 监听模态框
    $('#myModal').on('show.bs.modal', function (e) {
      // 清空表单的值
      $('#recipient-name').val('')
      $('#message-text').val('')

      // 将模态框的标题 类别 按钮更改
      const ModalText = $(e.relatedTarget).text().trim()
      $('.modal-title').text(ModalText)  //标题
      $('.category_name').text(ModalText) //类别名
      $('.btn-save').text(ModalText) //按钮

      // 判断点击编辑按钮 /admin/category/search
      if ($(e.relatedTarget).hasClass('btn-edit')) {
        window.id = $(e.relatedTarget).attr('dataid')
        // $(e.relatedTarget).parent().siblings().eq(0).text()  输出表单值
        // $('#recipient-name').val($(e.relatedTarget).parent().siblings().eq(0).text())
        // $('#message-text').val($(e.relatedTarget).parent().siblings().eq(1).text()) 
        $.ajax({
          url: 'http://localhost:8080/api/v1/admin/category/search',
          data: { id },
          success(res) {
            if (res.code === 200) {
              const { data } = res
              $('#recipient-name').val(data[0].name)
              $('#message-text').val(data[0].slug)
            }
          }
        })
      }
    })

    // /admin/category/add
    // /admin/category/edit
    // 新增按钮和编辑按钮
    $('.btn-save').on('click', function (e) {
      e.preventDefault()
      const name = $('#recipient-name').val().trim()
      const slug = $('#message-text').val().trim()
      if ($(this).text() == '新增分类') {
        if (name && slug) {
          $.ajax({
            url: 'http://192.168.16.90:8080/api/v1/admin/category/add',
            data: { name, slug },
            method: 'post',
            success(res) {
              alert(res.msg)
              if (res.code === 201) {
                $('#myModal').modal('hide')
                loadData()
              }
            }
          })
        }
      } else {
        if (name && slug) {
          $.ajax({
            url: 'http://192.168.16.90:8080/api/v1/admin/category/edit',
            data: { name, slug, id },
            method: 'post',
            success(res) {
              alert(res.msg)
              if (res.code === 200) {
                $('#myModal').modal('hide')
                loadData()
              }
            }
          })
        }
      }
    })

    // 删除按钮
    $('tbody').on('click', '.btn_del', function (e) {
      e.preventDefault()
      const id = $(this).attr('dataid')
      if (confirm('确认删除吗')) {
        $.ajax({
          url: 'http://localhost:8080/api/v1/admin/category/delete',
          data: { id },
          method: 'post',
          success(res) {
            console.log(res)
            if (res.code === 204) {
              alert(res.msg)
              loadData()
            }
          }
        })
      }
    })
  })
</script>